import { createRouter, createWebHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";
import type { App } from "vue";
import mainLayout from "@/layouts/mainLayout/index.vue";

/**
 * Note: 路由配置项
 *
 * hidden: true                     // 当设置 true 的时候该路由不会再侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1
 * alwaysShow: true                 // 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式--如组件页面
 *                                  // 只有一个时，会将那个子路由当做根路由显示在侧边栏--如引导页面
 *                                  // 若你想不管路由下面的 children 声明的个数都显示你的根路由
 *                                  // 你可以设置 alwaysShow: true，这样它就会忽略之前定义的规则，一直显示根路由
 * redirect: noRedirect             // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
 * name:'router-name'               // 设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
 * query: '{"id": 1, "name": "ry"}' // 访问路由的默认传递参数
 * meta : {
    noCache: true                   // 如果设置为true，则不会被 <keep-alive> 缓存(默认 false)
    title: 'title'                  // 设置该路由在侧边栏和面包屑中展示的名字
    icon: 'svg-name'                // 设置该路由的图标，对应路径src/assets/icons/svg
    breadcrumb: false               // 如果设置为false，则不会在breadcrumb面包屑中显示
    activeMenu: '/system/user'      // 当路由设置了该属性，则会高亮相对应的侧边栏。
  }
 */
  function getRouteParams() {
    const params = {};
    window.location.search.substring(1).split("&").forEach(function(pair) {
      pair = pair.split('=');
      params[pair[0]] = decodeURIComponent(pair[1]);
    });
    return params;
  }

export const constantRouterMap: RouteRecordRaw[] = [
  {
    path: "/",
    redirect: ""
  },
  {
    path: "/chatIm",
    name: "ChatIm",
    component: () => import("@/pages/chatIm/index.vue"),
    meta: { title: "" }
  },

  {
    path: "/home",
    component: mainLayout,
    children: [
      {
        path: "",
        name: "Home",
        component: () => import("@/pages/home/index.vue"),
        meta: { title: "首页", activeMenu: "Home", showNavBar: true }
      }
    ]
  },

  // 用户须知
  {
    path: "/servicePolicy",
    component: () => import("@/pages/agreement/servicePolicy.vue"),
    meta: {
      title: "用户须知",
    },
  },
  // 隐私政策
  {
    path: "/privacyPolicy",
    component: () => import("@/pages/agreement/privacyPolicy.vue"),
    meta: {
      title: "隐私政策",
    },
  },
  // {
  //   path: "/projectDetail",
  //   name: "ProjectDetails",
  //   component: baseLayout,
  //   meta: { title: "项目详情" },
  //   children: [
  //     {
  //       path: ":projectId",
  //       name: "ProjectDetail",
  //       component: () => import("@/pages/project/projectDetail/index.vue"),
  //       meta: { title: "项目详情" }
  //     }
  //   ]
  // },

  {
    path: "/:pathMatch(.*)*",
    redirect: ""
  }
];

const router = createRouter({
  history: createWebHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: (to, from, savedPosition) => {
    return { top: 0, left: 0 };
  }
});
export const resetRouter = (): void => {
  const resetWhiteNameList = ["Redirect", "Login", "NoFind", "Root"];
  router.getRoutes().forEach(route => {
    const { name } = route;
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name);
    }
  });
};

export const setupRouter = (app: App<Element>) => {
  app.use(router);
};

router.beforeEach((to, from, next) => {
  const {title} =getRouteParams()
  document.title = title || (to.meta?.title as string) || "";
  next();
});

export default router;
